import Vue from "vue";
import VueRouter from "vue-router";
// 引入路由组件
// 一级路由
const Index = () => import("../pages/index")
const Detail = () => import("../pages/detail")
const Login = () => import("../pages/login")
const Register = () => import("../pages/register")
const Order = () => import("../pages/order")
const Search = () => import("../pages/search")
const GoodList = () => import("../pages/goodlist")
const Addr = () => import("../pages/address")
const Pay = () => import("../pages/pay")
// 二级路由
const Home = () => import("../views/home")
const Class = () => import("../views/class")
const Cart = () => import("../views/cart")
const User = () => import("../views/user")
const Addaddr = () => import("../views/addaddr")

Vue.use(VueRouter)

let router = new VueRouter({
  routes: [
    {
      // 首页
      path: "/index",
      component: Index,
      children: [
        {
          path: "/home",
          component: Home,
          meta: {
            title: "小U商城"
          },
        },
        {
          path: "/class",
          component: Class,
          meta: {
            title: "分类"
          }
        },
        {
          path: "/cart",
          component: Cart,
          meta: {
            title: "购物车"
          }
        },
        {
          path: "/user",
          component: User,
          meta: {
            title: "个人中心"
          }
        },
        {
          path: "",
          redirect: "/home"
        }
      ]
    },
    {
      // 商品详情
      path: "/detail",
      component: Detail,
      meta: {
        title: "商品详情"
      }
    },
    {
      path: "/login",
      component: Login,
      meta: {
        title: "登录"
      }
    },
    {
      path: "/register",
      component: Register,
      meta: {
        title: "注册"
      }
    },
    {
      path: "/order",
      component: Order,
      meta: {
        title: "订单"
      }
    },
    {
      path: "/pay",
      component: Pay,
      meta: {
        title: "支付"
      }
    },
    {
      path: "/addr",
      component: Addr,
      meta: {
        title: "地址管理"
      },
      children: [
        {
          path: "/addaddr",
          component: Addaddr,
          meta: {
            title: "添加地址"
          }
        },
        {
          path: "/editaddr",
          component: Addaddr,
          meta: {
            title: "编辑地址"
          }
        }
      ]
    },
    {
      path: "/search",
      component: Search,
      meta: {
        title: "搜索"
      },
      children: [
        {
          path: "/goodlist",
          component: GoodList,
          meta: {
            title: "商品列表"
          }
        },
      ]
    },
    {
      // 重定向
      path: "*",
      redirect: "/index"
    }
  ],
  // 滚动行为
  scrollBehavior(to, from, savePosition) {
    if (savePosition) {
      return savePosition
    } else {
      return {
        x: 0,
        y: 0
      }
    }
  },
})
// router.beforeEach((to, from, next) => {
//   if (to.path == "/login" || to.path == "/register") {
//     next()
//     return
//   }
//   else {
//     if (sessionStorage.getItem("userinfo")) {
//       next()
//       return
//     }
//   }
//   next("/login")
// })

export default router